package com.hongxun.modules.api.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hongxun.modules.api.entity.BBudgetItemEntity;
import com.hongxun.modules.api.service.BBudgetItemService;
import com.hongxun.common.utils.PageUtils;
import com.hongxun.common.utils.Query;
import com.hongxun.common.utils.R;


/**
 * 预算项
 *
 * @author zkcn
 * @email youeremail@163.com
 * @date 2018-07-27 10:21:12
 */
@RestController
@RequestMapping("/api/bbudgetitem")
public class BBudgetItemController {
    @Autowired
    private BBudgetItemService bBudgetItemService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        String wheres = " 1=1 ";

        if (query.get("bIType") != null && !query.get("bIType").toString().equals("")) {
            wheres += " and  b_i_type = '" + query.get("bIType").toString() + "' ";
        }
        if (query.get("bIWork") != null && !query.get("bIWork").toString().equals("")) {
            wheres += " and  b_i_work = '" + query.get("bIWork").toString() + "' ";
        }
        if (query.get("bIItem") != null && !query.get("bIItem").toString().equals("")) {
            wheres += " and  b_i_item like '%" + query.get("bIItem").toString() + "%' ";
        }
        if (query.get("bIClass") != null && !query.get("bIClass").toString().equals("")) {
            wheres += " and  b_i_class like '%" + query.get("bIClass").toString() + "%' ";
        }
        if (query.get("aguid") != null && !query.get("aguid").toString().equals("")) {
            wheres += " and  b_guid = '" + query.get("aguid").toString() + "' ";
        }
        if (query.get("dClass") != null && !query.get("dClass").toString().equals("")) {
            wheres += " and  b_i_class = '" + query.get("dClass").toString() + "' ";
        }
        if (query.get("dItem") != null && !query.get("dItem").toString().equals("")) {
            wheres += " and  FIND_IN_SET(b_i_item,'" + query.get("dItem").toString() + "')";
        }
        if (query.get("dQuery") != null && !query.get("dQuery").toString().equals("") && !query.get("dQuery").toString().equals("null")) {
            wheres += " and  b_i_lower <= '" + query.get("dQuery").toString() + "' " + " and  b_i_high > '" + query.get("dQuery").toString() + "' ";
        }
        query.put("filterSql", wheres + " ");
        List<BBudgetItemEntity> bBudgetItemList = bBudgetItemService.queryList(query);
        int total = bBudgetItemService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(bBudgetItemList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    @RequestMapping("/select")
    public R select(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        String wheres = " 1=1 ";
        if (query.get("aguid") != null && !query.get("aguid").toString().equals("")) {
            wheres += " and  b_guid = '" + query.get("aguid").toString() + "' ";
        }

        query.put("filterSql", wheres + " ");
        List<BBudgetItemEntity> bBudgetItemList = bBudgetItemService.queryList(query);
        return R.ok().put("bBudgetItemList", bBudgetItemList);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{guid}")
    public R info(@PathVariable("guid") String guid) {
        BBudgetItemEntity bBudgetItem = bBudgetItemService.queryObject(guid);

        return R.ok().put("bBudgetItem", bBudgetItem);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody BBudgetItemEntity bBudgetItem) {
        bBudgetItemService.save(bBudgetItem);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody BBudgetItemEntity bBudgetItem) {
        bBudgetItemService.update(bBudgetItem);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] guids) {
        bBudgetItemService.deleteBatch(guids);

        return R.ok();
    }

}
